package com.k2data.app.k2bq.esafenet.utils;

import com.k2data.app.k2bq.esafenet.common.DatabaseEnum;
import com.k2data.app.k2bq.esafenet.common.GlobalVariables;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabaseUtils {

    private Statement statement;

    public DatabaseUtils(DatabaseEnum databaseEnum) throws SQLException {
        if (databaseEnum == DatabaseEnum.MYSQL) {
            statement = DriverManager.getConnection(GlobalVariables.MYSQL_URL, GlobalVariables.MYSQL_USER, GlobalVariables.MYSQL_PWD).createStatement();
        } else if (databaseEnum == DatabaseEnum.SQL_SERVER) {
            statement = DriverManager.getConnection(GlobalVariables.SQL_SERVER_URL, GlobalVariables.SQL_SERVER_USER, GlobalVariables.SQL_SERVER_PWD).createStatement();
        } else {
            throw new SQLException("databaseEnum error");
        }
    }

    public int doUpdate(String sql) throws SQLException {
        return statement.executeUpdate(sql);
    }

    public ResultSet doQuery(String sql) throws SQLException {
        return statement.executeQuery(sql);
    }

    public void close() throws SQLException {
        statement.close();
    }

    public void doSQLException(SQLException e) {
        System.out.println("SQLException: " + e.getMessage());
        System.out.println("SQLState: " + e.getSQLState());
        System.out.println("VendorError: " + e.getErrorCode());
    }

}
